package com.xuexiang.xui_lib.component.textview.supertextview;

import ohos.agp.components.AttrSet;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.text.Font;
import ohos.agp.utils.TextAlignment;
import ohos.agp.utils.TextTool;
import ohos.app.Context;

import com.xuexiang.xui_lib.component.textview.supertextview.adapter.HasTypeface;
import com.xuexiang.xui_lib.component.textview.supertextview.adapter.OfText;

/**
 * 基础TextView
 *
 * @author xuexiang
 * @since 2021-04-07
 */
public class BaseTextView extends DirectionalLayout implements HasTypeface {
    private Context mContext;
    private OfText mTopTextView, mCenterTextView, mBottomTextView;
    private ComponentContainer.LayoutConfig mTopTVParams, mCenterTVParams, mBottomTVParams;

    public BaseTextView(Context context) {
        super(context);
        init(context);
    }

    public BaseTextView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(context);
    }

    public BaseTextView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context);
    }

    private void init(Context context) {
        this.setOrientation(VERTICAL);
        mContext = context;
        initView();
    }

    private void initView() {
        initTopView();
        initCenterView();
        initBottomView();
    }

    private void initTopView() {
        if (mTopTVParams == null) {
            mTopTVParams = getParams(mTopTVParams);
        }
        if (mTopTextView == null) {
            mTopTextView = initTextView(mTopTVParams, mTopTextView);
        }
    }

    private void initCenterView() {
        if (mCenterTVParams == null) {
            mCenterTVParams = getParams(mCenterTVParams);
        }
        if (mCenterTextView == null) {
            mCenterTextView = initTextView(mCenterTVParams, mCenterTextView);
        }
    }

    private void initBottomView() {
        if (mBottomTVParams == null) {
            mBottomTVParams = getParams(mBottomTVParams);
        }
        if (mBottomTextView == null) {
            mBottomTextView = initTextView(mBottomTVParams, mBottomTextView);
        }
    }

    private OfText initTextView(ComponentContainer.LayoutConfig params, OfText textView) {
        textView = getTextView(textView, params);
        textView.setTextAlignment(TextAlignment.CENTER);
        addComponent(textView);
        return textView;
    }

    /**
     * 初始化textView
     *
     * @param textView 对象
     * @param layoutParams 对象
     * @return 返回
     */
    public OfText getTextView(OfText textView, ComponentContainer.LayoutConfig layoutParams) {
        if (textView == null) {
            textView = new OfText(mContext);
            textView.setLayoutConfig(layoutParams);
            textView.setVisibility(HIDE);
        }
        return textView;
    }

    /**
     * 初始化Params
     *
     * @param params 对象
     * @return 返回
     */
    public ComponentContainer.LayoutConfig getParams(ComponentContainer.LayoutConfig params) {
        if (params == null) {
            params = new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT,
                ComponentContainer.LayoutConfig.MATCH_CONTENT);
        }
        return params;
    }

    private void setTextString(Text textView, String textString) {
        textView.setText(textString);
        if (!TextTool.isNullOrEmpty(textString)) {
            textView.setVisibility(VISIBLE);
        }
    }

    public void setTopTextString(String s) {
        setTextString(mTopTextView, s);
    }


    public void setCenterTextString(String s) {
        setTextString(mCenterTextView, s);
    }

    public void setBottomTextString(String s) {
        setTextString(mBottomTextView, s);
    }

    public Text getTopTextView() {
        return mTopTextView;
    }

    public Text getCenterTextView() {
        return mCenterTextView;
    }

    public Text getBottomTextView() {
        return mBottomTextView;
    }

    public void setMaxEms(int topMaxEms, int centerMaxEms, int bottomMaxEms) {
        mTopTextView.setTruncationMode(Text.TruncationMode.ELLIPSIS_AT_END);
        mCenterTextView.setTruncationMode(Text.TruncationMode.ELLIPSIS_AT_END);
        mBottomTextView.setTruncationMode(Text.TruncationMode.ELLIPSIS_AT_END);

        mTopTextView.setMaxLength(topMaxEms);
        mCenterTextView.setMaxLength(centerMaxEms);
        mBottomTextView.setMaxLength(bottomMaxEms);
    }

    public void setCenterSpaceHeight(int centerSpaceHeight) {
        mTopTVParams.setMargins(0, 0, 0, centerSpaceHeight / 2);
        mCenterTVParams.setMargins(0, centerSpaceHeight / 2, 0, centerSpaceHeight / 2);
        mBottomTVParams.setMargins(0, centerSpaceHeight / 2, 0, 0);
    }

    @Override
    public void setTypeface(Font font) {
        if (mTopTextView != null) {
            mTopTextView.setTypeface(font);
        }
        if (mCenterTextView != null) {
            mCenterTextView.setTypeface(font);
        }
        if (mBottomTextView != null) {
            mBottomTextView.setTypeface(font);
        }
    }
}